class_name HitBox
extends Node2D

## TODO Area2D和Detector的控制应该跟属性控制一样，每个控制都有单独的ID

#region 属性
@export var detectors:Array[HitBoxAreaDetector] = []
var areas:Array[Area2D] = []
var detector_dict:Dictionary = {}
var inited:bool = false
#endregion
#region 字段
var _entity:
	get:
		if not _entity:
			var combat_component:CombatComponent = Utils.find_parent_by_type(self,CombatComponent,-1)
			if combat_component:
				_entity = combat_component.entity
		return _entity
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init():
	areas.clear()
	for child in get_children():
		if child is Area2D:
			#child.collision_layer = 0
			areas.append(child)
	for detector in detectors:
		detector_dict[detector.detector_id] = detector
		detector.init_detector(self)
	inited = true
func get_entity() -> Entity:
	return _entity
func get_area2d(index:int) -> Area2D:
	if index < areas.size():
		return areas[index]
	return null
func get_area2d_by_path(path:NodePath) -> Area2D:
	return get_node(path)
func get_detector(detector_id:int) -> HitBoxAreaDetector:
	return detector_dict.get(detector_id)
func set_area2d_active(index:int,is_active:bool):
	if index < areas.size():
		areas[index].monitoring = is_active
		areas[index].monitorable = is_active
func set_area2d_active_by_path(path:NodePath,is_active:bool):
	var area = get_node(path)
	if area in areas:
		area.monitoring = is_active
		area.monitorable = is_active
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _process(delta: float) -> void:
	if not inited:
		return
	for detector in detectors:
		if detector.active and detector.timing == GameEnum.Area2DDetectorTiming.Stay and detector.on_detect_target.get_connections().size()>0:
			detector.detect_update()
#endregion
